package com.knoya.chejian.pda.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.knoya.chejian.pda.Myapp;
import com.knoya.chejian.pda.myclass.UnCeHandler;
import com.knoya.chejian.pda.util.Sputil;

import java.util.List;

/**
 * Created by Administrator on 2016/12/31/031.
 * 作者：刘洋
 */
public abstract class  BaseActivity extends AppCompatActivity {
    public String activityname="";
    public SharedPreferences.Editor editor;//sp储存器
    public SharedPreferences sp;//sp提取器
    public BroadcastReceiver mBoradcastReceiver;
    public int jiemian = 1;
    Myapp application;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UnCeHandler.activity=activityname;
        //        透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //        透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        application = (Myapp) getApplication();
//        application.init();
        application.addActivity(this);
        editor = Sputil.editor("pda");
        sp = Sputil.sp("pda");
        jiemian = sp.getInt("jiemian",1);
        if(jiemian == 2){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        }
        setContentView(getLayoutId());
        initCreate();
    }
    //加载页面布局文件
    protected abstract int getLayoutId();
    //oncreate
    protected abstract void initCreate();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        application.removeActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver();
        UnCeHandler.activity=activityname;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (isFastDoubleClick()) {
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private long lastClickTime=System.currentTimeMillis();
    private  boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (timeD >= 0 && timeD <= 1000) {
            return true;
        } else {
            lastClickTime = time;
            return false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unRegisterReceiver();
    }

    private void registerReceiver()
    {
        if(mBoradcastReceiver != null){
            IntentFilter mFilter= new IntentFilter("nlscan.action.SCANNER_RESULT");
            mFilter.addAction("android.intent.ACTION_DECODE_DATA");
            mFilter.addAction("com.qs.scancode");
            registerReceiver(mBoradcastReceiver, mFilter);
            Intent intent = new Intent ("ACTION_BAR_SCANCFG");
            intent.putExtra("EXTRA_SCAN_MODE", 3);
            getApplicationContext().sendBroadcast(intent);
        }
    }


    private void unRegisterReceiver()
    {
        if(mBoradcastReceiver != null) {
            try {
                unregisterReceiver(mBoradcastReceiver);
            } catch (Exception e) {
            }
        }
    }

}
